<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermission('1','0')">添加权限</el-button>
        </div>
        <el-table border :data="permissionlist" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" size="small" type="text" @click="addPermission('2',row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="getPermissionDetailAPIFn(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermissionListAPIFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="ShowTitle" :close-on-click-modal="false" @close="closeDialog">
      <!-- 表单内容 -->
      <el-form ref="perForm" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="confirmAddpermission">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { transListToTreeData } from '@/utils/index'
import { getPermissionListAPI, addPermissionListAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionListAPI } from '@/api/permission'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      permissionlist: [],
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: ['blur'] }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: ['blur'] }
        ],
        description: [
          { required: true, message: '权限描述不能为空', trigger: ['blur'] }
        ]
      }
    }
  },
  computed: {
    ShowTitle() {
      return this.formData.id ? '编辑权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionListAPIFn()
  },
  methods: {
    closeDialog() {
      this.showDialog = false
      this.$refs.perForm.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: ''
      }
    },
    // 表格数据渲染
    async getPermissionListAPIFn() {
      const { data } = await getPermissionListAPI()
      this.permissionlist = transListToTreeData(data, '0')
    },
    // 添加
    // 一级权限添加
    addPermission(type, id) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = id
    },
    confirmAddpermission() {
      this.$refs.perForm.validate(async valid => {
        if (!valid) return
        // 当有id时进行修改逻辑
        if (this.formData.id) {
          await updatePermissionAPI(this.formData)
          this.$message.success('修改成功')
        } else {
          // 添加逻辑
          await addPermissionListAPI(this.formData)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.getPermissionListAPIFn()
      })
    },
    // 修改（数据回填）
    async getPermissionDetailAPIFn(id) {
      this.showDialog = true
      const { data } = await getPermissionDetailAPI(id)
      this.formData = data
    },
    // 删除
    delPermissionListAPIFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      }).then(async() => {
        await delPermissionListAPI(id)
        this.$message.success('删除成功')
        this.getPermissionListAPIFn()
      }).catch(() => {
        this.$message.error('删除失败')
      })
    }
  }
}
</script>
